package com.iplant.mes.controller.sfc;

import com.alibaba.fastjson.JSON;
import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.aps.service.po.aps.APSTaskPartPointDay;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.SFCFrockBindService;
import com.iplant.mes.service.SFCService;
import com.iplant.mes.service.po.constants.MESConstants;
import com.iplant.mes.service.po.sfc.SFCStationInterfaceLog;
import com.iplant.mes.service.po.sfc.SFCTaskSelf;
import com.iplant.mes.service.po.sfc.SFCWorkpieceRecord;
import com.iplant.mes.service.po.sfc.autoStation.SFCDeviceReport;
import com.iplant.mes.service.po.sfc.autoStation.SFCDeviceSelfData;
import com.iplant.mes.service.po.sfc.autoStation.SFCStatusFeedback;
import com.iplant.mes.service.po.sfc.autoStation.SFCTaskSelfParam;
import com.iplant.pdm.service.po.bop.partpoint.FPCRouteStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/api/SFCTaskSelf")
public class SFCTaskSelfController extends BasicController<SFCTaskSelf> {
    private static final Logger logger = LoggerFactory.getLogger(SFCTaskSelfController.class);

    public SFCTaskSelfController() {
        super(SFCTaskSelf.class);
    }

    @Autowired
    SFCService mSFCService;

    @Autowired
    SFCFrockBindService mSFCFrockBindService;

    /**
     * 派工（保存派工结果）
     */
    @PostMapping("/SaveDispatchResult")
    public Object SaveDispatchResult(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            List<SFCTaskSelf> wSFCTaskSelfList = CloneTool.CloneArray(wParam.get("data"), SFCTaskSelf.class);
            if (wSFCTaskSelfList == null || wSFCTaskSelfList.size() == 0
                    || wSFCTaskSelfList.stream().anyMatch(p -> p.WorkerID <= 0 || p.ProductID <= 0 || p.OrderID <= 0)
                    || wSFCTaskSelfList.stream().anyMatch(p -> p.TaskPartPointDayID <= 0)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_SaveDispatchResult(wLoginUser, wSFCTaskSelfList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查询打卡任务（待办/已办）
     */
    @GetMapping("/QueryClockTask")
    public Object QueryClockTask(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<SFCTaskSelf>> wServiceResult = mSFCService.SFC_QueryClockTask(wLoginUser, wParams,
                    wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据工位、检验类型查询检验项点列表
     */
    @GetMapping("/QueryItemsByStation")
    public Object QueryItemsByStation(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            int wCheckType = StringUtils.parseInt(request.getParameter("CheckType"));

            if (wStationID <= 0 || wCheckType <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCRouteStep>> wServiceResult = mSFCService.SFC_QueryItemsByStation(wLoginUser,
                    wStationID, wCheckType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据工位、检验类型、派工单查询检验项点列表
     */
    @PostMapping("/QueryItemsByStationAndTask")
    public Object QueryItemsByStationAndTask(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wStationID = StringUtils.parseInt(wParam.get("StationID"));
            int wCheckType = StringUtils.parseInt(wParam.get("CheckType"));
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("SFCTaskSelf"), SFCTaskSelf.class);
            //是否为抽检
            boolean wIsSpotCheck = StringUtils.parseInt(wParam.get("IsSpotCheck")) == 1;
            if (wStationID <= 0 || wCheckType <= 0 || wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCRouteStep>> wServiceResult = mSFCService.SFC_QueryItemsByStationAndTask(wLoginUser,
                    wStationID, wCheckType, wSFCTaskSelf, wIsSpotCheck);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据订单、工艺、工序查询专检检验项点列表
     */
    @GetMapping("/QueryCheckItemsByOrder")
    public Object QueryCheckItemsByOrder(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wRoutePartID = StringUtils.parseInt(request.getParameter("RoutePartID"));
            int wRoutePartPointID = StringUtils.parseInt(request.getParameter("RoutePartPointID"));

            if (wOrderID <= 0 || wRoutePartID <= 0 || wRoutePartPointID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCRouteStep>> wServiceResult = mSFCService.SFC_QueryCheckItemsByOrder(wLoginUser,
                    wOrderID, wRoutePartID, wRoutePartPointID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 下达日计划
     */
    @PostMapping("/IssuePlanList")
    public Object IssuePlanList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            List<APSTaskPartPointDay> wAPSTaskPartPointDayList = CloneTool.CloneArray(wParam.get("data"),
                    APSTaskPartPointDay.class);
            if (wAPSTaskPartPointDayList == null || wAPSTaskPartPointDayList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.APS_IssueDayPlanList(wLoginUser,
                    wAPSTaskPartPointDayList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 4.6	工单数据获取
     */
    @GetMapping("/Current")
    public Object Current(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wStationCode = StringUtils.parseString(request.getParameter("StationCode"));
            int wIsAll = StringUtils.parseInt(request.getParameter("IsAll"));
            int wIsUserAll = StringUtils.parseInt(request.getParameter("IsUserAll"));

            if (StringUtils.isEmpty(wStationCode)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<SFCTaskSelf>> wServiceResult = mSFCService.SFC_QuerySFCTaskSelfCurrent(wLoginUser,
                    wStationCode, wIsAll, wIsUserAll);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 4.7	工件开工检验
     */
    @PostMapping("/PrevCheck")
    public Object PrevCheck(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            SFCTaskSelfParam wSFCTaskSelfParam = CloneTool.Clone(wParam.get("data"), SFCTaskSelfParam.class);
            if (wSFCTaskSelfParam == null || wSFCTaskSelfParam.TaskSelfID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            Calendar wRequestTime = Calendar.getInstance();

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_SFCTaskSelfParamPrevCheck(wLoginUser,
                    wSFCTaskSelfParam);

            Calendar wResponseTime = Calendar.getInstance();

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }

            //获取工单
            SFCTaskSelf wSFCTaskSelf = (SFCTaskSelf) wServiceResult.CustomResult.get("SFCTaskSelf");

            //维护工位实时接口日志
            Optional<SFCStationInterfaceLog> wOption =
                    MESConstants.StationInterfaceLogList.stream().filter(p -> p.StationID == wSFCTaskSelf.StationID
                            && p.InterfaceType.equals("工件开工检查")).findFirst();
            if (wOption.isPresent()) {
                SFCStationInterfaceLog sfcStationInterfaceLog = wOption.get();
                sfcStationInterfaceLog.OperatorID = wLoginUser.ID;
                sfcStationInterfaceLog.OperatorName = wLoginUser.Name;
                sfcStationInterfaceLog.InterfaceInput = JSON.toJSONString(wParam);
                sfcStationInterfaceLog.InterfaceOutput = JSON.toJSONString(wResult);
                sfcStationInterfaceLog.RequestTime = wRequestTime;
                sfcStationInterfaceLog.ResponseTime = wResponseTime;
            } else {
                SFCStationInterfaceLog wSFCStationInterfaceLog =
                        new SFCStationInterfaceLog(wSFCTaskSelf.StationID, wSFCTaskSelf.StationName,
                                wSFCTaskSelf.StationCode, wLoginUser.ID, wLoginUser.Name,
                                JSON.toJSONString(wParam),
                                JSON.toJSONString(wResult), wRequestTime,
                                wResponseTime
                                , "工件开工检查");
                MESConstants.StationInterfaceLogList.add(wSFCStationInterfaceLog);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 4.8工单报工
     */
    @PostMapping("/Report")
    public Object Report(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            SFCDeviceReport wSFCDeviceReport = CloneTool.Clone(wParam.get("data"),
                    SFCDeviceReport.class);
            if (wSFCDeviceReport == null || wSFCDeviceReport.TaskSelfID <= 0 || wSFCDeviceReport.ReportComNum <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_DeviceReport(wLoginUser, wSFCDeviceReport);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 4.9工单状态反馈
     */
    @PostMapping("/UpdateProduct")
    public Object UpdateProduct(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            SFCStatusFeedback wSFCStatusFeedback = CloneTool.Clone(wParam.get("data"),
                    SFCStatusFeedback.class);
            if (wSFCStatusFeedback == null || StringUtils.parseInt(wSFCStatusFeedback.ID) <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_DeviceUpdateProduct(wLoginUser,
                    wSFCStatusFeedback);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 4.11工单批量自检
     */
    @PostMapping("/BatchSelfCheck")
    public Object BatchSelfCheck(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            List<SFCDeviceSelfData> wSFCDeviceSelfDataList = CloneTool.CloneArray(wParam.get("data"),
                    SFCDeviceSelfData.class);
            if (wSFCDeviceSelfDataList == null || wSFCDeviceSelfDataList.size() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_DeviceBatchSelfCheck(wLoginUser,
                    wSFCDeviceSelfDataList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 4.12 托盘加当前工位获取当前工位绑定工单信息列表
     */
    @GetMapping("/QueryTaskListByTrayAndStation")
    public Object QueryTaskListByTrayAndStation(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wTrayCode = StringUtils.parseString(request.getParameter("TrayCode"));
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));

            if (StringUtils.isEmpty(wTrayCode) || wStationID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<SFCTaskSelf>> wServiceResult =
                    mSFCFrockBindService.SFC_QueryTaskListByTrayAndStation(wLoginUser, wTrayCode, wStationID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 4.13 托盘获取产品信息 包含 订单号
     */
    @GetMapping("/QueryProductInfoByTray")
    public Object QueryProductInfoByTray(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wTrayCode = StringUtils.parseString(request.getParameter("TrayCode"));
            String wMouldCode = StringUtils.parseString(request.getParameter("MouldCode"));

            if (StringUtils.isEmpty(wTrayCode) && StringUtils.isEmpty(wMouldCode)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<SFCWorkpieceRecord> wServiceResult =
                    mSFCFrockBindService.SFC_QueryProductInfoByTray(wLoginUser, wTrayCode, wMouldCode);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 删除派工单
     */
    @Override
    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("data"), SFCTaskSelf.class);
            if (wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_DeleteTaskSelf(wLoginUser, wSFCTaskSelf);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询所有派工单
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request, mClazz);

            ServiceResult<List<SFCTaskSelf>> wServiceResult = mSFCFrockBindService.SFC_QuerySFCTaskSelfAll(wLoginUser
                    , wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 通过工位查询设备列表
     */
    @GetMapping("/QueryDeviceListByWorkstation")
    public Object QueryDeviceListByWorkstation(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            if (wStationID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<DMSDeviceLedger>> wServiceResult =
                    mSFCFrockBindService.SFC_QueryDeviceListByWorkstation(wLoginUser, wStationID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
